import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'navBar.dart';

class Login extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<Login> {
  final _formKey = GlobalKey<FormState>();
  String _user, _password;
  bool _isObscure = true;
  Color _eyeColor;
  var _result = '';
  var _decodeResult = '';
  loadDataByDio() async {
    try {
      print('登陆中');
      Response response;
      Dio dio = new Dio();
      response = await dio.get(
          "接口地址?LOGIN_NAME=$_user&PASSWORD=$_password");
      if (response.statusCode == 200) {
        _decodeTest(response.data);
      } else {
        _result = 'error code : ${response.statusCode}';
      }
    } catch (exception) {
      print('exc:$exception');
      _result = '网络异常';
    }

    setState(() {});
  }

  _decodeTest(var body) {
    bool usertxt = body['success'];
    String errMess = body['message'];
    if (usertxt == false) {
      _modalBottomSheetMenu(errMess);
    } else {
      Navigator.of(context).pushAndRemoveUntil(
          new MaterialPageRoute(builder: (context) => new NavBar()),
          (route) => route == null);
    }
  }

  _modalBottomSheetMenu(errMess) {
    showModalBottomSheet(
        context: context,
        builder: (builder) {
          return new Container(
            height: 50.0,
            child: new Container(
                decoration: new BoxDecoration(
                    color: Colors.grey,
                ),
                child: new Center(
                  child: new Text(errMess,style: TextStyle(color: Colors.white),),
                )),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Form(
            key: _formKey,
            child: Container(
                decoration: new BoxDecoration(
                  image: new DecorationImage(
                      image: new AssetImage('images/shebei_bg.png'),
                      fit: BoxFit.fill),
                ),
                child: ListView(
                  padding: EdgeInsets.symmetric(horizontal: 20.0),
                  children: <Widget>[
                    SizedBox(
                      height: 100.0,
                    ),
                    buildTitle(),
                    SizedBox(height: 70.0),
                    buildUserTextField(),
                    SizedBox(height: 10.0),
                    buildPasswordTextField(context),
                    buildForgetPasswordText(context),
                    SizedBox(height: 10.0),
                    buildLoginButton(context),
                  ],
                ))));
  }

  Align buildLoginButton(BuildContext context) {
    return Align(
      child: SizedBox(
        height: 45.0,
        width: 270.0,
        child: RaisedButton(
          child: Text(
            '登录',
            style: TextStyle(color: Colors.white, fontSize: 18.0),
          ),
          color: Color.fromARGB(255, 64, 200, 176),
          // onPressed: _loadDataByDio,
          onPressed: () {
            if (_formKey.currentState.validate()) {
              //只有输入的内容符合要求通过才会到达此处
              _formKey.currentState.save();
              loadDataByDio();
            }
          },
          shape: StadiumBorder(side: BorderSide.none),
        ),
      ),
    );
  }

  Container buildForgetPasswordText(BuildContext context) {
    return Container(
        width: 500,
        padding: const EdgeInsets.only(top: 3.0),
        child: Row(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.fromLTRB(0, 2, 0, 0),
              child: FlatButton(
                child: Text(
                  '注册帐号',
                  style: TextStyle(fontSize: 16.0, color: Colors.white),
                ),
                onPressed: () {
                  // Navigator.pop(context);
                  Navigator.push(
                    context,
                    new MaterialPageRoute(builder: (context) => new AddUser()),
                  );
                },
              ),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(128, 2, 0, 0),
              child: FlatButton(
                child: Text(
                  '忘记密码',
                  style: TextStyle(fontSize: 16.0, color: Colors.white),
                ),
                onPressed: () {
                  // Navigator.pop(context);
                  Navigator.push(
                    context,
                    new MaterialPageRoute(
                        builder: (context) => new ForgetPassword()),
                  );
                },
              ),
            ),
          ],
        ));
  }

  TextFormField buildPasswordTextField(BuildContext context) {
    return TextFormField(
      onSaved: (String value) => _password = value,
      obscureText: _isObscure,
      validator: (String value) {
        if (value.isEmpty) {
          return '请输入密码';
        }
      },
      style: TextStyle(color: Colors.white, fontSize: 18.0),
      cursorColor: Colors.white,
      decoration: InputDecoration(
          contentPadding: EdgeInsets.only(top: 13),
          hintText: '密码',
          hintStyle: TextStyle(color: Colors.white, fontSize: 18.0),
          prefixIcon: passlogo,
          suffixIcon: IconButton(
              icon: Icon(
                Icons.remove_red_eye,
                color: _eyeColor,
              ),
              onPressed: () {
                setState(() {
                  _isObscure = !_isObscure;
                  _eyeColor = _isObscure
                      ? Color.fromARGB(255, 166, 166, 166)
                      : Color.fromARGB(255, 41, 41, 41);
                });
              })),
    );
  }

  TextFormField buildUserTextField() {
    return TextFormField(
      validator: (String value) {
        if (value.isEmpty) {
          return '请输入用户名';
        }
      },
      style: TextStyle(color: Colors.white, fontSize: 18.0),
      cursorColor: Colors.white,
      decoration: InputDecoration(
        contentPadding: EdgeInsets.only(top: 13),
        hintText: '账号',
        hintStyle: TextStyle(color: Colors.white, fontSize: 18.0),
        prefixIcon: userlogo,
      ),
      onSaved: (String value) => _user = value,
    );
  }

  Padding buildTitle() {
    return Padding(
      padding: EdgeInsets.all(8.0),
      child: titlelogo,
    );
  }
}
